package cn.gudian.cms.Message

class SmsRecordController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    params.sort= params.sort?params.sort:"id"
      params.order= params.order?params.order:"desc"

   
    [smsRecordInstanceList: SmsRecord.list(params), smsRecordInstanceTotal: SmsRecord.count()]
  }

  def create = {
    def smsRecordInstance = new SmsRecord()
    smsRecordInstance.properties = params
    return [smsRecordInstance: smsRecordInstance]
  }

  def save = {
    def smsRecordInstance = new SmsRecord(params)
    if (!smsRecordInstance.hasErrors() && smsRecordInstance.save()) {
      flash.message = "smsRecord.created"
      flash.args = [smsRecordInstance.id]
      flash.defaultMessage = "SmsRecord ${smsRecordInstance.id} created"
      redirect(action: "show", id: smsRecordInstance.id)
    }
    else {
      render(view: "create", model: [smsRecordInstance: smsRecordInstance])
    }
  }

  def show = {
    def smsRecordInstance = SmsRecord.get(params.id)
    if (!smsRecordInstance) {
      flash.message = "smsRecord.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SmsRecord not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [smsRecordInstance: smsRecordInstance]
    }
  }

  def edit = {
    def smsRecordInstance = SmsRecord.get(params.id)
    if (!smsRecordInstance) {
      flash.message = "smsRecord.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SmsRecord not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [smsRecordInstance: smsRecordInstance]
    }
  }

  def update = {
    def smsRecordInstance = SmsRecord.get(params.id)
    if (smsRecordInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (smsRecordInstance.version > version) {

          smsRecordInstance.errors.rejectValue("version", "smsRecord.optimistic.locking.failure", "Another user has updated this SmsRecord while you were editing")
          render(view: "edit", model: [smsRecordInstance: smsRecordInstance])
          return
        }
      }
      smsRecordInstance.properties = params
      if (!smsRecordInstance.hasErrors() && smsRecordInstance.save()) {
        flash.message = "smsRecord.updated"
        flash.args = [params.id]
        flash.defaultMessage = "SmsRecord ${params.id} updated"
        redirect(action: "show", id: smsRecordInstance.id)
      }
      else {
        render(view: "edit", model: [smsRecordInstance: smsRecordInstance])
      }
    }
    else {
      flash.message = "smsRecord.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SmsRecord not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def smsRecordInstance = SmsRecord.get(params.id)
    if (smsRecordInstance) {
      try {
        smsRecordInstance.delete()
        flash.message = "smsRecord.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "SmsRecord ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "smsRecord.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "SmsRecord ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "smsRecord.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "SmsRecord not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        SmsRecord.executeUpdate("delete SmsRecord where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
}
